# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:张广勤
@Web site: https://www.tunan.wang
@Github:www.github.com
 
@File:chrome_version_wmic1_0-error.py
@Time:2024/10/30 8:49

@Motto:不积跬步无以至千里，不积小流无以成江海！
"""

import subprocess
import re

def get_chrome_version_wmic():
    try:
        # 使用wmic命令查找Chrome的安装路径
        result = subprocess.run(
            ['wmic', 'path', 'win32_product', 'where', 'name="Google Chrome"', 'get', 'InstallLocation'],
            stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)

        if result.returncode != 0:
            print(f"Error: {result.stderr.strip()}")
            return None

        # 解析安装路径
        install_locations = result.stdout.strip().split('\n\n')
        if len(install_locations) < 2 or not install_locations[1].strip():
            print("Chrome not found or unable to determine install location.")
            return None

        install_location = install_locations[1].strip()
        chrome_exe = install_location + r'\Application\chrome.exe'

        # 使用命令行获取版本号
        result = subprocess.run([chrome_exe, '--version'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)

        if result.returncode == 0:
            version_info = result.stdout.strip()
            match = re.search(r'Google Chrome (\d+\.\d+\.\d+\.\d+)', version_info)
            if match:
                return match.group(1)
            else:
                print("Unable to parse Chrome version from output.")
                return None
        else:
            print(f"Error: {result.stderr.strip()}")
            return None

    except Exception as e:
        print(f"An error occurred: {e}")
        return None

chrome_version = get_chrome_version_wmic()
if chrome_version:
    print(f"Google Chrome Version: {chrome_version}")
else:
    print("Unable to determine Google Chrome version.")